<?php
class searchController extends Controller{
	private $model;
	public $keyword;
	public function actionIndex(){
		$searchModel = new SearchForm;
		$dataProvider = null;
		$userInputKeyword = '';
		if(isset($_POST['SearchForm'])){
			$searchModel->attributes = $_POST['SearchForm'];
			$userInputKeyword = $_POST['SearchForm']['searchword'];
			$this->keyword = $searchModel->splitword();
			array_push($this->keyword,$_POST['SearchForm']['searchword']);
			$this->keyword = array_unique($this->keyword);
			if($this->keyword){
				//$this->redirect(Yii::app()->searchForm->returnUrl);
				$dataProvider = $this->newSearch($this->keyword);
			}	
		}
		$this->render('index',array('dataProvider'=>$dataProvider,'userInputKeyword'=>$userInputKeyword));
	}
	public function actionUser(){
		$searchModel = new SearchForm;
		$dataProvider = null;
		$userInputKeyword = '';
		if(isset($_POST['SearchForm'])){
			$this->keyword = $_POST['SearchForm']['searchword'];
			$userInputKeyword = $_POST['SearchForm']['searchword'];
			if($this->keyword){
				$dataProvider = $this->userSearch($this->keyword);
			}	
		}
		$this->render('friend',array('searchModel'=>$searchModel,'dataProvider'=>$dataProvider,'userInputKeyword'=>$userInputKeyword));
	}
	public function userSearch($keywords){
		$criteria = new CDbCriteria(array(
			'condition'=>'user_name like "%'.$keywords.'%" or favor_name like "%'.$keywords.'%"',
			'order'=>'user_id asc',
		));
		$dataProvider = new CActiveDataProvider('User',array(
			'criteria'=>$criteria,
		));
		return $dataProvider;
	}
	public function favorSearch($keywords){
		$criteria = new CDbCriteria(array(
			'condition'=>'favor_name like "%'.$keywords.'%"',
			'order'=>'user_count asc',
		));
		$dataProvider = new CActiveDataProvider('Favor',array(
			'criteria'=>$criteria,
		));
		return $dataProvider;
	}
	public function getKeyword(){
		return $this->keyword;
	}
	public function newSearch($keywords){
		$keywordNum = count($keywords);
		if($keywordNum==1){
			$criteria = new CDbCriteria(array(
				//site_name 标题查询\site_desc 内容查询\site_keyword标签查询
				'condition'=>'site_name like "%'.$keywords[0].'%" or site_desc like "%'.$keywords[0].'%" or site_keyword like "%' . $keywords[0] . '%"',
				'order'=>'post_time desc',
			));
		}else{
			$sql0 = '';
			$sql1 = '';
			$sql2 = '';
			for($i=0;$i<$keywordNum;$i++){
				$sql0.=" site_name like '%".trim($keywords[$i])."%'"." or";
			}
			for($j=0;$j<$keywordNum;$j++){
				$sql1.=" site_desc like '%".trim($keywords[$j])."%'"." or";
			}
			for($j=0;$j<$keywordNum;$j++){
				$sql2.=" site_keyword like '%".trim($keywords[$j])."%'"." or";
			}
			$sql2 = substr($sql2,0,-3);
			for($i=0;$i<$num;$i++){
				$sql.=" site_keyword like '%".trim($keywords[$i])."%'"." or";
			}
			$sql = substr($sql,0,-3);
			$criteria = new CDbCriteria(array(
				'condition'=>$sql0.$sql1.$sql2,
				'order'=>'post_time desc',
			));
		}
		$dataProvider = new CActiveDataProvider('Links',array(
			'criteria'=>$criteria,
		));
		return News::dataProvider($dataProvider,'link_id','link_url');
	}
	public function tagSearch($keywords){
		$sql = '';
		$num=count($keywords);
		if(count($keywords)==1){
			$criteria = new CDbCriteria(array(
				'condition'=>'news_tag like "%'.$keywords[0].'%"',
				'order'=>'post_time desc',
			));
		}else{
			for($m=0;$m<$num;$m++){
				$sql.=" news_tag like '%".trim($keywords[$m])."%'"." or";
			}	
			$criteria = new CDbCriteria(array(
				'condition'=>$sql,
				'order'=>'post_time desc',
			));
			$dataProvider = new CActiveDataProvider('News',array(
				'criteria'=>$criteria,
			));
		}
		return $dataProvider;
	}
	public function actionAjax(){
		//$msg = "月份创下了150万名玩家注册的记录。据悉，金山软件计划将于第三季度对《剑网3》开启正式公测";
		//$sub = "金山软件发布公告称旗下西山居工作室首款自主研发";
		$_POST['subjectenc'] = isset($_POST['subjectenc'])?$_POST['subjectenc']:'';
		$_POST['messageenc'] = isset($_POST['messageenc'])?$_POST['messageenc']:'';
		$subjectenc = rawurlencode(strip_tags(urldecode($_POST['subjectenc'])));
		$messageenc = rawurlencode(strip_tags(preg_replace("/\[.+?\]/U", '', urldecode($_POST['messageenc']))));
		//dbg($messageenc);
		$data = @implode('', file("http://keyword.discuz.com/related_kw.html?title=$subjectenc&content=$messageenc&ics=utf-8&ocs=utf-8"));
		//dbg($data);
		if($data) {
			//dbg($data);
			$parser = xml_parser_create();
			xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
			xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
			xml_parse_into_struct($parser, $data, $values, $index);
			xml_parser_free($parser);

			$kws = array();

			foreach($values as $valuearray) {
				if($valuearray['tag'] == 'kw' || $valuearray['tag'] == 'ekw') {
						$kws[] = trim($valuearray['value']);
				}
			}
			NC::json(true,'success',$kws);
		} else {
			//showmessage(' ');
			NC::json(false,'error',$kws);
		}
		//print_r($kws);
	}
}
?>
